require'cgi'
require'diff'
class WriteboardsController < ApplicationController
before_filter :check_authorization
  layout "project",:except=>['wiki','save_wiki']
  # GET /writeboards
  # GET /writeboards.xml
  def index
    if params[:category_id].to_i > 0
      @writeboard_pages, @writeboards= paginate :writeboards, :per_page => 10, :conditions=>["project_id = ? and category_id = ?",params[:project_id], params[:category_id]],:order => "id desc"
    elsif params[:category_id] == "uncat"
      @writeboard_pages, @writeboards= paginate :writeboards, :per_page => 10,:conditions=>["project_id = ? and (category_id is null or category_id = 0)",params[:project_id]],:order => "id desc"
    else
      @writeboard_pages, @writeboards= paginate :writeboards, :per_page => 10,:conditions=>["project_id = ?",params[:project_id]],:order => "id desc"
    end
    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @writeboards.to_xml }
    end
  end

  # GET /writeboards/1
  # GET /writeboards/1.xml
  def show
    @writeboard = Writeboard.find(params[:id])
    @wiki = Wiki.find(params[:wiid]) if params[:wiid]

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @writeboard.to_xml }
    end
  end
  def diff
    @writeboard = Writeboard.find(params[:diff][:wid])
    @wiki      = Wiki.find(params[:version].split("_").first)
    @wiki_from = Wiki.find(params[:version_from].split("_").first)
    @diff = WikiDiff.new(@wiki.content,@wiki_from.content) 
  end

  # GET /writeboards/new
  def new
    @writeboard = Writeboard.new
    @cats = @project.wb_categories
  end

  # GET /writeboards/1;edit
  def edit
    @writeboard = Writeboard.find(params[:id])
  end

  def wiki 
    @writeboard = Writeboard.find(params[:wrid])
    @wiki = Wiki.find(params[:wiid]) if params[:wiid]
    @cats = @project.wb_categories
  end

  # POST /writeboards
  # POST /writeboards.xml
  def create
    @writeboard = Writeboard.new(params[:writeboard])
    @writeboard.user = @cur_user
    @writeboard.category_id = params[:category_id]
    respond_to do |format|
      if @writeboard.save
        wiki = Wiki.new
	wiki.content = params[:content]
	wiki.user = @cur_user
	wiki.writeboard = @writeboard
	wiki.save

        flash[:notice] = 'Writeboard was successfully created.'
        format.html { redirect_to writeboards_url(params[:project_id]) }
        format.xml  { head :created, :location => writeboard_url(@writeboard) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @writeboard.errors.to_xml }
      end
    end
  end

  # PUT /writeboards/1
  # PUT /writeboards/1.xml
  def update
    @writeboard = Writeboard.find(params[:id])

    respond_to do |format|
      if @writeboard.update_attributes(params[:writeboard])
        flash[:notice] = 'Writeboard was successfully updated.'
        format.html { redirect_to writeboard_url(@writeboard) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @writeboard.errors.to_xml }
      end
    end
  end

  # DELETE /writeboards/1
  # DELETE /writeboards/1.xml
  def destroy
    @writeboard = Writeboard.find(params[:id])
    @writeboard.destroy

    respond_to do |format|
      format.html { redirect_to writeboards_url }
      format.xml  { head :ok }
    end
  end
  def save_wiki
    content = params[:wiki_content] 
    wiki = Wiki.new(params[:wiki])
    wiki.content = content
    wiki.save
    @writeboard = Writeboard.find(params[:wiki][:writeboard_id])
    @writeboard.category_id = params[:category_id]
    @writeboard.save
    @cats = @project.wb_categories
  end
  def change_name
    wr = Writeboard.find(params[:wrid])
    wr.name = params[:name]
    wr.save
    render :update do |page|
      page.replace_html 'wname',params[:name]
      page.visual_effect :highlight,:wname
      page.hide 'indicator_rname'
    end
  end
end
